package com.tulingxueyuan.mall.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsBrand;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsBrandMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsBrandExample;
import com.tulingxueyuan.mall.modules.pms.model.dto.PmsBrandParamDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2022-02-24
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;

    /**
     * 品牌数据列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum,pageSize);
        QueryWrapper<PmsBrand> QueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)){
            QueryWrapper.lambda().like(PmsBrand::getName,keyword);
        }
        QueryWrapper.lambda().orderByAsc(PmsBrand::getSort);


        return this.page(page,QueryWrapper);
    }

    /**
     * 添加品牌信息
     * @param pmsBrand
     * @return
     */
    @Override
    public boolean createBrand(PmsBrandParamDTO pmsBrand) {
        PmsBrand pmsBrand1 = new PmsBrand();
        BeanUtils.copyProperties(pmsBrand, pmsBrand1);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        return brandMapper.insertSelective(pmsBrand);
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @Override
    public boolean deleteBrand(Long id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量更新品牌是否显示
     * @param ids
     * @param showStatus
     * @return
     */

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setShowStatus(showStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample)>0;


    }


    /**
     * 批量更新品牌制造商是否显示
     * @param ids
     * @param factoryStatus
     * @return
     */
    @Override
    public boolean updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setFactoryStatus(factoryStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample)>0;
    }
}
